package com.ruoyi.pm.forest;

import com.dtflys.forest.Forest;
import com.dtflys.forest.http.ForestRequest;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pm.domain.ExcPort;
import com.ruoyi.pm.domain.ExcPortHeader;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class CommonForest {
    public Map<String,Object> getForest(ExcPort excPort, Map<String,Object> inRequest, List<ExcPortHeader> excPortHeaderList){
        // 创建 GET 请求对象
        ForestRequest<?> request = Forest.get(excPort.getServerPath());

        if(StringUtils.isNotEmpty(excPortHeaderList)){
            for (ExcPortHeader excPortHeader : excPortHeaderList){
                request.addHeader(excPortHeader.getField(), excPortHeader.getFieldValue());
            }
        }
        if(StringUtils.isNotNull(inRequest) && inRequest.size() >0){
            request.addBody(inRequest);
        }
        // 手动执行发送请求，并以字符串形式接受响应数据
        return request.execute(Map.class);
    }

    public Map<String,Object> postForest(ExcPort excPort, Map<String,Object> inRequest, List<ExcPortHeader> excPortHeaderList){
        // 创建 GET 请求对象
        ForestRequest<?> request = Forest.post(excPort.getServerPath());

        if(StringUtils.isNotEmpty(excPortHeaderList)){
            for (ExcPortHeader excPortHeader : excPortHeaderList){
                request.addHeader(excPortHeader.getField(), excPortHeader.getFieldValue());
            }
        }
        if(StringUtils.isNotNull(inRequest) && inRequest.size() >0){
            request.addBody(inRequest);
        }
        // 手动执行发送请求，并以字符串形式接受响应数据
        return request.execute(Map.class);
    }

    public Map<String,Object> fromDataForest(ExcPort excPort, Map<String,Object> inRequest, List<ExcPortHeader> excPortHeaderList){
        // 创建 GET 请求对象
        ForestRequest<?> request = Forest.post(excPort.getServerPath());

        if(StringUtils.isNotEmpty(excPortHeaderList)){
            for (ExcPortHeader excPortHeader : excPortHeaderList){
                request.addHeader(excPortHeader.getField(), excPortHeader.getFieldValue());
            }
        }
        if(StringUtils.isNotNull(inRequest) && inRequest.size() >0){
            request.addQuery(inRequest);
        }
        // 手动执行发送请求，并以字符串形式接受响应数据
        return request.execute(Map.class);
    }
}
